package com.leantech.android.browser.ui.shortcut.adapter

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.cscj.android.repository.db.entity.ShortCutEntity
import com.cscj.android.repository.uidata.ShortCutUIData
import com.leantech.android.browser.databinding.ItemLinkShortCutBinding
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.show
import splitties.dimensions.dip

class LinkShortCutAdapter(private val linkItemCallback: LinkItemAdapter.LinkItemCallback) :
    ListAdapter<ShortCutUIData, LinkShortCutAdapter.LinkShortCutViewHolder>(
        diffCallback
    ) {

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<ShortCutUIData>() {
            override fun areItemsTheSame(
                oldItem: ShortCutUIData, newItem: ShortCutUIData
            ): Boolean {
                return oldItem.category.id == newItem.category.id
            }

            override fun areContentsTheSame(
                oldItem: ShortCutUIData, newItem: ShortCutUIData
            ): Boolean {
                return oldItem == newItem
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LinkShortCutViewHolder {
        val binding =
            ItemLinkShortCutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return LinkShortCutViewHolder(binding)
    }

    override fun onBindViewHolder(holder: LinkShortCutViewHolder, position: Int) {
        holder.bind(getItem(position), linkItemCallback)
    }

    override fun onBindViewHolder(
        holder: LinkShortCutViewHolder, position: Int, payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
    }

    class LinkShortCutViewHolder(private val binding: ItemLinkShortCutBinding) :
        ViewHolder(binding.root) {

        fun bind(data: ShortCutUIData, linkItemCallback: LinkItemAdapter.LinkItemCallback) {
            var adapter = binding.recyclerView.adapter as? LinkItemAdapter
            if (adapter == null) {
                adapter = LinkItemAdapter(linkItemCallback)
                binding.recyclerView.adapter = adapter
                val spanCount = 5
                binding.recyclerView.layoutManager = GridLayoutManager(itemView.context, spanCount)
                binding.recyclerView.addItemDecoration(object : ItemDecoration() {
                    override fun getItemOffsets(
                        outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
                    ) {
                        outRect.top = itemView.context.dip(16)
                        outRect.bottom = itemView.context.dip(16)
                    }
                })
            }
            updateShortCutList(data.shortCutList)
            binding.title.text = data.category.name
            val expanded = true
            binding.btnToggle.isSelected = expanded //展开
            binding.recyclerView.show(expanded)

            binding.btnToggle.onThrottleClick {
                it.isSelected = !it.isSelected
                binding.recyclerView.show(it.isSelected) //展开
            }
        }

        fun updateShortCutList(list: List<ShortCutEntity>) {
            val adapter = binding.recyclerView.adapter as? LinkItemAdapter ?: return
            adapter.submitList(list)
        }

    }

}